package com.it.base.common.mybatis;

import java.util.Date;

import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.it.base.common.constant.SystemConstant;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 * @version 1.0
 **/
@Slf4j
@Configuration
@MapperScan("com.it.base.**.mapper")
public class MybatisPlusConfig implements MetaObjectHandler {

	/**
	 * 分页插件，自动识别数据库类型
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		log.info("mybatis分页插件初始化成功！");
		return paginationInterceptor;
	}

	/**
	 * 启用性能分析插件
	 */
	@Bean
	public PerformanceInterceptor performanceInterceptor() {
		PerformanceInterceptor p = new PerformanceInterceptor();
		log.info("mybatis分页插件初始化成功！");
		return p;
	}

	@Override
	public void insertFill(MetaObject metaObject) {
		this.setFieldValByName("createTime", new Date(), metaObject);
		this.setFieldValByName("updateTime", new Date(), metaObject);
//		 setFieldValByName("createdBy", UserInfoContext.getUserInfo().getId(), metaObject);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		this.setFieldValByName("updateTime", new Date(), metaObject);
	}

}
